package com.example.administrator.familfinalcialmanagement.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GoodsBean;
import com.example.administrator.familfinalcialmanagement.databinding.FragmentIndexBinding;
import com.example.administrator.familfinalcialmanagement.ui.activity.MyOrdersActivity;
import com.example.administrator.familfinalcialmanagement.ui.activity.TaskDescActivity;
import com.example.administrator.familfinalcialmanagement.ui.adapter.FriendAdapter;
import com.example.administrator.familfinalcialmanagement.ui.adapter.GoodsAdapter;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.GlideUtil;
import com.example.administrator.familfinalcialmanagement.util.GsonUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/26.
 */

public class IndexFragment extends BaseLazyFragment {

    FragmentIndexBinding binding;
    List<GoodsBean.DataBean> list = new ArrayList<>();
    List<GoodsBean.DataBean> listSecond = new ArrayList<>();
    GoodsAdapter adapter;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_index, container, false);
        binding.setFragment(this);
        return binding.getRoot();
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    protected void onVisible() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        adapter = new GoodsAdapter(getActivity(),list);
        binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener( new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), TaskDescActivity.class);
                intent.putExtra( "module_id" ,list.get( position ).getId() + "" );
                intent.putExtra( "module_type","id" );
                intent.putExtra( "module_title",list.get( position ).getType_name() );
                startActivity( intent );
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        } );

        ApiHTTP.getInstance().getGoods(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                ToastUtil.showToast("获取商品成功");

                GoodsBean goodsBean = GsonUtil.GsonToBean(s,GoodsBean.class);
                if (0 == goodsBean.getCode()) {
                    listSecond = goodsBean.getData();
                    list.clear();
                    list.addAll(listSecond);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                ToastUtil.showToast("获取商品失败");
            }
        });

        binding.tvModuleSearch.setOnClickListener( this );
    }


    @Override
    protected void lazyLoad() {

    }

    /**
     * 得到轮播图的View
     *
     * @param context
     * @param url
     * @return
     */
    public static View getImageView(Context context, String url) {
        RelativeLayout rl = new RelativeLayout(context);
        ImageView imageView = new ImageView(context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(layoutParams);
        GlideUtil.setCircleMethod(context, url, imageView);
        ImageView backGround = new ImageView(context);
        backGround.setLayoutParams(layoutParams);
        backGround.setBackgroundResource(R.color.cycle_image_bg);
        rl.addView(imageView);
        rl.addView(backGround);
        return rl;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvModuleSearch:
                String search = binding.etModuleSearch.getText().toString().trim();
                if (TextUtils.isEmpty( search )) {
                    ToastUtil.showToast( "请输入搜索内容" );
                    return;
                }

                Intent intent = new Intent(getActivity(), TaskDescActivity.class);
                intent.putExtra( "module_like" ,search);
                intent.putExtra( "module_type","search" );
                intent.putExtra( "module_title","搜索" );
                startActivity( intent );

                break;
                default:
        }
    }
}
